package com.adinnet.admin.childStudyToday.controller;


import com.adinnet.admin.childStudyToday.model.ChildStudyToday;
import com.adinnet.admin.childStudyToday.model.form.ChildStudyTodayForm;
import com.adinnet.admin.childStudyToday.model.vo.ChildStudyTodayResp;
import com.adinnet.admin.childStudyToday.service.ChildStudyTodayService;
import com.adinnet.admin.classs.service.ClasssService;
import com.adinnet.admin.common.service.CommonService;
import com.adinnet.admin.organization.service.OrganizationService;
import com.adinnet.admin.school.service.SchoolService;
import com.adinnet.admin.system.model.vo.SysUserInfo;
import com.adinnet.core.PageResult;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * <p>
 * 今日学习时间 前端控制器
 * </p>
 *
 * @author chenjia
 * @since 2020-12-16
 */
@RestController
@RequestMapping("admin/childStudyToday")
public class ChildStudyTodayController {

    @Autowired
    private CommonService commonService;

    @Autowired
    private OrganizationService organizationService;

    @Autowired
    private SchoolService schoolService;

    @Autowired
    private ClasssService classsService;

    @Autowired
    private ChildStudyTodayService childStudyTodayService;

    /**
     * 学习时间页面
     * 1，返回用户可查询的机构列表
     * 2，返回用户可查询的校区列表
     * 3，返回用户可查询的班级列表
     * @param model
     * @return
     */
    @GetMapping()
    public ModelAndView childStudyToday(Model model){
        SysUserInfo sysUserInfo=commonService.getLoginUser();
        model.addAttribute("organizationsList", organizationService.getListByUserRole(sysUserInfo));
        model.addAttribute("schoolList", schoolService.getListByUserRole(sysUserInfo));
        model.addAttribute("classsList", classsService.getListByUserRole(sysUserInfo));
        return new ModelAndView("childStudyToday/childStudyToday.html");
    }

    /**
     * 学习时间列表
     * 默认页面
     * 1，超级管理员查所有
     * 2，机构查机构下的数据
     * 3，校区查校区的数据
     * 4，班级查班级的数据
     * @param cstForm
     * @return
     */
    @GetMapping("/list")
    public PageResult<ChildStudyTodayResp> list(ChildStudyTodayForm cstForm) {
        SysUserInfo sysUser= commonService.getLoginUser();
        //默认页面处理和重置搜索的处理
        if(StringUtils.isBlank(cstForm.getSearchDate())){
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            cstForm.setSearchDate(simpleDateFormat.format(new Date()));
        }
        if(cstForm.getClassId()==null&&cstForm.getOrganizationId()==null&&cstForm.getSchoolId()==null){
            if(sysUser!=null){
                switch (sysUser.getMaxRole()){
                    case "admin":
                        break;
                    case "organization":
                        cstForm.setOrganizationId(sysUser.getOrganizationId());
                        break;
                    case "school":
                        cstForm.setSchoolId(sysUser.getSchoolId());
                        break;
                    case "classs":
                        cstForm.setSysUserId(sysUser.getId());
                        break;
                    default:
                        break;
                }
            }
        }
        return childStudyTodayService.queryList(cstForm);
    }

}
